//Homework //Complete the three functions that contain the //TODO comment. You should be able to use the //logic found in PlayerWonAcross to complete //PlayerWonDown, PlayerWonDownRight, and PlayerWonDownLeft. #include #include using namespace std; const int ROWS = 6; const int COLUMNS = 7; const int WIN_COUNT = 4; const char PLAYER_ONE = (char)1; const char PLAYER_TWO = (char)2; void InitializeGameBoard(char gameBoard[][COLUMNS]) { for(int row = 0 ; row < ROWS; row++) { for(int column = 0 ; column < COLUMNS; column++) { gameBoard[row][column] = ' '; } } } void DisplayGameBoard(char gameBoard[][COLUMNS]) { cout << "1234567\n"; for(int row = 0 ; row < ROWS; row++) { for(int column = 0 ; column < COLUMNS; column++) { cout << gameBoard[row][column]; } cout << endl; } } bool ColumnFull(char gameBoard[][COLUMNS], int move) { return (gameBoard[0][move-1] != ' '); } void MakeMove(char gameBoard[][COLUMNS], char currentPlayer) { int move; //get valid move do { cout << currentPlayer << "'s Turn >"; cin >> move; } while(move < 1 || move > COLUMNS || ColumnFull(gameBoard, move)); //place piece bool piecePlaced = false; for(int row = ROWS; row >= 0 && !piecePlaced; row--) { if(gameBoard[row][move-1] == ' ') { gameBoard[row][move-1] = currentPlayer; piecePlaced = true; } } } void ChangeTurn(char& currentPlayer) { if(currentPlayer == PLAYER_ONE) { currentPlayer = PLAYER_TWO; } else { currentPlayer = PLAYER_ONE; } } bool BoardFull(char gameBoard[][COLUMNS]) { bool result = true; for(int column = 0 ; column < COLUMNS; column++) { if( gameBoard[0][column] == ' ' ) { result = false; } } return result; } bool PlayerWonAcross(char gameBoard[][COLUMNS], char player) { bool result = false; for(int row = 0 ; row < ROWS; row++) { for(int column = 0 ; column <= COLUMNS - WIN_COUNT; column++) { //There must be WIN_COUNT picece in a row to win. //I am assuming that the player has won starting / //at the current positon and going across WIN_COUNT spots. //If I find that any of those spots do not contain the //players piece the player did not win there. bool foundLocalWin = true; for(int offSet = 0; offSet < WIN_COUNT; offSet++) { if(gameBoard[row][column + offSet] != player) { foundLocalWin = false; } } if(foundLocalWin) { result = true; } } cout << endl; } return result; } bool PlayerWonDown(char gameBoard[][COLUMNS], char player) { bool result = false; //TODO: finish this function return result; } bool PlayerWonDownRight(char gameBoard[][COLUMNS], char player) { bool result = false; //TODO: finish this function return result; } bool PlayerWonDownLeft(char gameBoard[][COLUMNS], char player) { bool result = false; //TODO: finish this function return result; } bool PlayerWon(char gameBoard[][COLUMNS], char player) { bool result = false; if(PlayerWonAcross(gameBoard,player) || PlayerWonDown(gameBoard,player) || PlayerWonDownRight(gameBoard,player) || PlayerWonDownLeft(gameBoard,player)) { result = true; } return result; } bool GameOver(char gameBoard[][COLUMNS]) { bool result = false; if(PlayerWon(gameBoard,PLAYER_ONE) || PlayerWon(gameBoard,PLAYER_TWO) || BoardFull(gameBoard) ) { result = true; } return result; } void AnnounceWinner(char gameBoard[][COLUMNS]) { if(PlayerWon(gameBoard,PLAYER_ONE)) { cout << PLAYER_ONE << " Is the winner" << endl; } else if(PlayerWon(gameBoard,PLAYER_TWO)) { cout << PLAYER_TWO << " Is the winner" << endl; } else { cout << "The board was filled without a winner" << endl; } } void main() { char gameBoard[ROWS][COLUMNS]; char currentPlayer = PLAYER_ONE; InitializeGameBoard(gameBoard); do { DisplayGameBoard(gameBoard); MakeMove(gameBoard, currentPlayer); ChangeTurn(currentPlayer); } while(!GameOver(gameBoard)); DisplayGameBoard(gameBoard); AnnounceWinner(gameBoard); }